using Modding;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace HelloMods.Healths
{
    public class HealthPoint : UnityEngine.MonoBehaviour
    {

        public List<HealthStatus> bars = new();

        private static Modding.ILogger logger => HelloMod.LoadedInstance;
        private Settings settings = Settings.Instance;
        public HealthPoint()
        {

        }


        internal void Attack(HealthStatus status)
        {
            if (!settings.ShowHealth)
                return;
            if (!bars.Contains(status))
                bars.Add(status);
        }

        internal void AddEnemy(GameObject enemy, HealthManager health)
        { 
            this.enabled = true; 
            var dt = enemy.AddComponent<HealthStatus>();
            dt.healthManager = health;
            dt.healthPoint = this;
       //     logger.Log($"add enemy: {enemy}=>{dt.Current},{dt.Max},{dt.Tick}");
        }



        Texture2D bgTexture = null;
        Texture2D emptyTexture = null;
        Texture2D hpTexture = null;
        Texture2D deltaTexture = null;
        GUIStyle fontStyle = new GUIStyle();

        // Start is called before the first frame update
        void Start()
        {
            fontStyle.normal.background = null;
            fontStyle.normal.textColor = Color.white;
            fontStyle.alignment = TextAnchor.MiddleLeft;
            bgTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            bgTexture.SetPixel(0, 0, new Color(1, 1, 1, 1f));
            bgTexture.Apply();

            emptyTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            emptyTexture.SetPixel(0, 0, new Color(0, 0, 0, 0.5f));
            emptyTexture.Apply();

            hpTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            hpTexture.SetPixel(0, 0, new Color(1, 0, 0, 1f));
            hpTexture.Apply();

            deltaTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            deltaTexture.SetPixel(0, 0, new Color(0.5f, 0, 0));
            deltaTexture.Apply();
        }


        private void OnGUI()
        {


            var height = (Screen.height * 0.03f);
            var left = 30;
            var top = 10;
            var width = (Screen.width - left * 2);

            var t = Time.time;
            var skip = 0.2f;
            var duration = 0.5f;
            bars.RemoveAll(v => t - v.Tick > 5);
            fontStyle.fontSize = (int)(height * 0.6f);
            //此处将所有敌人的血条计算到一行中
            var count = bars.Count;
            if (count == 0) return;
            GUILayout.BeginVertical();
            var w = width / count;      //每一个敌人的血条总宽度


            for (var i = 0; i < count; i++)
            {
                var v = bars[i];

                if (t >= v.BeginTime + skip && t <= v.BeginTime + skip + duration)
                    v.Delta = v.BeginDelta + (v.Current - v.BeginDelta) * (t - v.BeginTime - skip) / duration;
                else if (t >= v.BeginTime + skip + duration)
                    v.Delta = v.Current;

                var percent1 = Mathf.Max(0, v.Current * 100f / v.Max, 0);
                var percent2 = Mathf.Max(v.Delta * 100f / v.Max, 0);


                var l = left + i * w + i * 5;
                var up = top;
                var rect1 = new Rect(l, up, w, height);
                var rect2 = new Rect(l + 2, up + 2, w - 4, height - 4);
                var rect3 = new Rect(l + 2, up + 2, (w - 4) * percent2 / 100f, height - 4);
                var rect4 = new Rect(l + 2, up + 2, (w - 4) * percent1 / 100f, height - 4);

                GUI.DrawTexture(rect1, bgTexture, ScaleMode.StretchToFill, false, 1.0f);
                GUI.DrawTexture(rect2, emptyTexture, ScaleMode.StretchToFill, false, 1.0f);

                GUI.DrawTexture(rect3, deltaTexture, ScaleMode.StretchToFill, false, 1.0F);

                GUI.DrawTexture(rect4, hpTexture, ScaleMode.StretchToFill, false, 1.0F);

                fontStyle.alignment = TextAnchor.MiddleRight;
                GUI.Label(rect1, $"-{Math.Abs(v.DamageInOne)}  ", fontStyle);
                fontStyle.alignment = TextAnchor.MiddleLeft;

                GUI.Label(rect1, $"  {v.Name} {v.Current}/{v.Max}", fontStyle);
            }


            GUILayout.EndVertical();
        }

    }
}
